package com.imooc.oa.controller;

import com.imooc.oa.entity.Department;
import com.imooc.oa.entity.Employee;
import com.imooc.oa.entity.Node;
import com.imooc.oa.entity.User;
import com.imooc.oa.service.DepartmentService;
import com.imooc.oa.service.EmployeeService;
import com.imooc.oa.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
//设置用户界面的控制器
@WebServlet(name = "IndexServlet",urlPatterns="/index")
public class IndexServlet extends HttpServlet {
    private UserService userService=new UserService();
    private EmployeeService employeeService=new EmployeeService();
    private DepartmentService departmentService=new DepartmentService();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        //获得当前登录用户对象
        User user=(User)session.getAttribute("login_user");
        //获得员工信息
        Employee employee=employeeService.selectById(user.getEmployeeId());
        //获得当前员工的部门信息
        Department department=departmentService.selectById(employee.getDepartmentId());
        //获取登录用户可用功能模块列表
        List<Node> nodeList= userService.selectByUserId(user.getUserId());
        //放入请求属性,只需要在index.ftl中使用所以放在请求中就可以了
        request.setAttribute("node_list", nodeList);
        //将当前登录的员工信息和部门信息放在HttpSession中，不放在request是因为使用范围不止在index.ftl中。
        session.setAttribute("current_employee", employee);
        session.setAttribute("current_department", department);
        //请求派发至ftl进行展现
        request.getRequestDispatcher("/index.ftl").forward(request, response);

    }
}
